package com.usian.search.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.search.dtos.UserSearchDto;
import com.usian.model.search.pojos.ApUserSearch;
import com.usian.model.user.pojos.ApUser;
import com.usian.search.feign.BehaviorFeign;
import com.usian.search.mapper.ApUserSearchMapper;
import com.usian.search.service.ApUserSearchService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Log4j2
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {
    @Autowired
    private BehaviorFeign behaviorFeign;
    @Override
    public ResponseResult findUserSearch(UserSearchDto userSearchDto) {
        //校验参数
        if (userSearchDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取登录用户
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //获取行为实体
        ApBehaviorEntry behaviorEntry = behaviorFeign.findByUserIdOrEntryId(user.getId(), userSearchDto.getEquipmentId());
        if (behaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页查询
        Page page = new Page(userSearchDto.getPageNum(), userSearchDto.getPageSize());
        IPage page1 = page(page, Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, behaviorEntry.getId()).eq(ApUserSearch::getStatus, 1));
        ResponseResult result = new PageResponseResult(userSearchDto.getPageNum(), userSearchDto.getPageSize(), (int) page1.getTotal());
        result.setData(page1.getRecords());
        return result;
    }

    @Override
    @Async("taskExecutor")
    public void insert(Integer entryId, String searchWords) {
        ApUserSearch search = getOne(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, entryId).eq(ApUserSearch::getKeyword, searchWords));
        if (search!=null&& search.getStatus()==1){
            log.info("当前关键字已存在，无需再次保存");
            return;
        }else if(search!=null&& search.getStatus()==0){
            search.setStatus(1);
            updateById(search);
            return;
        }
        //3.如果不存在，保存新的数据
        search = new ApUserSearch();
        search.setEntryId(entryId);
        search.setStatus(1);
        search.setKeyword(searchWords);
        search.setCreatedTime(new Date());
        save(search);
    }
}
